﻿using Bricks.Business.Models.Account;
using Bricks.Business.Service;
using Bricks.Component.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Component.Utils.Extensions;

namespace Bricks.Site.Web.Controllers
{
    /// <summary>
    /// 功能管理Controller
    /// </summary>
    [Export]
    public class ActionInfoController : BaseController
    {
        [Import]
        public IActionInfoService ActionService { get; set; }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewBag.PanelInfo = base.GetPanelInfo();  //取得面板信息
            ViewBag.ToolBarInfo = base.GetToolBarInfo();  //取得工具栏信息
            return View();
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="rows">显示行数</param>
        /// <param name="page">当前页数</param>
        /// <returns></returns>
        public ActionResult GetDataList(int rows = 20, int page = 1, bool isFilterDeleted = false)  //默认会传这两个参数
        {
            ////int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            ////int pageSize = Request["rows"] == null ? 20 : int.Parse(Request["rows"]);

            //var queryData = ActionService.Actions.OrderBy(m => m.OrderNo).Skip(rows * (page - 1)).Take(rows);
            ////var data = from m in queryData
            ////           select new
            ////           {
            ////               m.ID,
            ////               m.ActionName,
            ////               m.ActionCode,
            ////               m.ActionTypeNum,
            ////               m.Icon,
            ////               m.OrderNo,
            ////               m.Description,
            ////               m.IsDeleted,
            ////               m.AddDate
            ////           };
            //var result = new { total = ActionService.Actions.Count(), rows = queryData };
            //return Json(result, "text/html");

            var queryData = ActionService.Actions.OrderBy(m => m.OrderNo).ToList();  //转换为List之后，可进行函数操作
            var data = from m in queryData
                       select new
                       {
                           m.Icon,
                           m.ID,
                           m.ActionName,
                           m.ActionCode,
                           ActionTypeString = GetActionTypeString(m.ActionType),  //获取菜单类型的中文描述
                           m.OrderNo,
                           m.IsDeleted,
                           m.Description,
                           m.AddDate,
                       };
            if (isFilterDeleted)
            {
                data = data.Where(m => m.IsDeleted == false);
            }
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取菜单类型的中文描述
        /// </summary>
        /// <param name="actionType"></param>
        /// <returns></returns>
        public string GetActionTypeString(ActionType actionType)
        {
            string result = string.Empty;
            switch (actionType)
            {

                case ActionType.Function: result = ActionType.Function.ToDescription(); break;
                case ActionType.Purview: result = ActionType.Purview.ToDescription(); break;
            }
            return result;
        }

        /// <summary>
        /// 启用或禁用
        /// </summary>
        /// <returns></returns>
        public ActionResult Used()
        {
            string id = Request["ID"];
            string isUsed = Request["IsUsed"];

            Guid guidID = Guid.Parse(id);
            var record = ActionService.Actions.FirstOrDefault(m => m.ID == guidID);
            record.IsDeleted = isUsed == "false" ? true : false;

            var result = ActionService.Modify(record);

            if (result.ResultType == OperationResultType.Success)
                return View(result.AppendData);

            return Content(id + isUsed);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            Guid guidID = id.ToGuid();
            var exeResult = ActionService.Delete(guidID);
            return Json(exeResult);
        }

        /// <summary>
        /// 新增或编辑
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            ActionInfo model = new ActionInfo();
            //如果是编辑
            if (!String.IsNullOrEmpty(id))
            {
                Guid guidID = id.ToGuid();
                var record = ActionService.Actions.FirstOrDefault(m => m.ID == guidID);
                if (record != null)
                {
                    model = record;
                }
            }
            //如果是新增
            else
            {
                model.ActionTypeNum = 0;  //类型默认为空Null
            }
            return View(model);
        }

        /// <summary>
        /// 新增或编辑（响应）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(ActionInfo model)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            var record = ActionService.Actions.FirstOrDefault(m => m.ID == model.ID);

            //新增
            if (record == null)
            {
                result = ActionService.Add(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //修改
            else
            {
                result = ActionService.Modify(model);
                //if (result.ResultType == OperationResultType.Success)
                //    return View(result.AppendData);
            }
            //return View();
            return Json(result, "text/html");
        }

        /// <summary>
        /// 设置禁用或启用
        /// </summary>
        /// <param name="id">记录ID</param>
        /// <param name="isEnable">是否启用</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetEnable(string id, string isEnable)
        {
            var result = new OperationResult(OperationResultType.NoChanged);
            Guid guidID = id.ToGuid();
            var record = ActionService.Actions.FirstOrDefault(m => m.ID == guidID);
            if (record != null)
            {
                record.IsDeleted = isEnable == "False";
                //UserView view = new UserView();
                //view = record.CopyTo<UserView>();
                result = ActionService.Modify(record);
                if (result.ResultType == OperationResultType.Success)
                    result.Message = (isEnable == "True" ? "启用" : "禁用") + "成功";
            }
            return Json(result);
            //return View();
        }

        /// <summary>
        /// 排序设置
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="order">排序</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SetOrder(string id, string order)
        {
            Guid guidID = id.ToGuid();
            var exeResult = ActionService.SetOrder(guidID, order);
            return Json(exeResult);
        }
    }
}
